package game.session;

import java.util.Collection;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.HallServer;
import game.entity.PlayingRole;
import game.module.battleReport.dao.BattleReportCache;
import game.module.battleReport.dao.PlayerReportCache;
import game.module.dabt.dao.DabtCache;
import game.module.hall.dao.SettingCache;
import game.module.item.dao.ItemCache;
import game.module.mail.dao.MailCache;
import game.module.offline.bean.PlayerOfflineCache;
import game.module.offline.logic.PlayerOfflineManager;
import game.module.turntables.dao.TurntableCache;
import game.module.user.dao.ActivitystateCache;
import game.module.user.dao.PlayerDao;

public class CharacterDownlineManager {

	public static final Logger logger = LoggerFactory.getLogger(CharacterDownlineManager.class);

	static class SingletonHolder {
		static CharacterDownlineManager instance = new CharacterDownlineManager();
	}

	public static CharacterDownlineManager getInstance() {
		return SingletonHolder.instance;
	}

	public void removeCache(PlayingRole playingRole) {
		logger.info("stage status reset!,id={}", playingRole.getId());
		// 关卡中处理
//		if (playingRole.getPlayerCacheStatus().getPosition() == PlayerPosition.PLAYER_POSITION_STAGE
//				|| playingRole.getPlayerCacheStatus().getPosition() == PlayerPosition.PLAYER_POSITION_TOWER) {
//			StagePveManager.getInstance().downlineStat(playingRole);
//		} else if (playingRole.getPlayerCacheStatus().getPosition() == PlayerPosition.PLAYER_POSITION_BOSS) {
//			BossLogManager.getInstance().downlineBoss(playingRole);
//		}
		// playingRole.getPlayerCacheStatus().setPosition(PlayerPosition.PLAYER_POSITION_HALL);

		logger.info("remove player cache!,id={}", playingRole.getId());
		// 逻辑移除
		int playerId = playingRole.getId();
		SettingCache.getInstance().remove(playerId);
		ItemCache.getInstance().remove(playerId);
		TurntableCache.getInstance().remove(playerId);
		ActivitystateCache.getInstance().remove(playerId);
		DabtCache.getInstance().remove(playerId);
		MailCache.getInstance().remove(playerId);
		PlayerReportCache.getInstance().remove(playerId);
		BattleReportCache.getInstance().playerDownline(playerId);
	}

	public void savePlayerData(final PlayingRole playingRole) {
		logger.info("persist player data!,id={}", playingRole.getId());
		// saveHeroData(playingRole.getPlayerBean());
		updateOnlineDuration(playingRole);
		if (playingRole.getLevelGoTimeout() != null) {
			playingRole.getLevelGoTimeout().cancel();
		}
		if (playingRole.getLoginTimeout() != null) {
			playingRole.getLoginTimeout().cancel();
		}
		// update player offline cache
		PlayerOfflineCache playerOfflineCache = PlayerOfflineManager.getInstance()
				.getPlayerOfflineCache(playingRole.getId());
		if (playerOfflineCache != null) {
			PlayerOfflineManager.getInstance().updatePlayerOffline(playingRole, playerOfflineCache);
		}
//		playingRole.getPlayerBean().setDownlineTime(new Date());
//		// 移除自动加体力
//		PlayerLogic.getInstance().removeAddStrengthSchedule(playingRole.getId());
		// 保存玩家数据
		if (HallServer.executorService != null) {
			HallServer.executorService.execute(new Runnable() {
				@Override
				public void run() {
					PlayerDao.getInstance().updatePlayerBean(playingRole.getPlayerBean());
				}
			});
		}
	}

	private void updateOnlineDuration(PlayingRole playingRole) {
		Date enterGameTime = playingRole.getPlayerCacheStatus().getEnterGameTime();
		if (enterGameTime != null) {
			final long timeDiff = (System.currentTimeMillis() - enterGameTime.getTime()) / 1000;
			final int playerId = playingRole.getId();
			HallServer.executorService.execute(new Runnable() {
				@Override
				public void run() {
//					boolean isExist = LogOnlineDurationDao.getInstance().checkLogOnlineDurationExist(playerId);
//					if (isExist) {
//						LogOnlineDurationDao.getInstance().updateLogOnlineDuration(playerId, (int) timeDiff);
//					} else {
//						LogOnlineDurationDao.getInstance().insertLogOnlineDuration(playerId, (int) timeDiff);
//					}
				}
			});
		}
	}

	/**
	 * 服务器停机保存数据
	 */
	public void serverShutdown() {
		Collection<PlayingRole> playingRoles = SessionManager.getInstance().getAllPlayers();
		for (PlayingRole playingRole : playingRoles) {
			PlayerDao.getInstance().updatePlayerBean(playingRole.getPlayerBean());
			updateOnlineDuration(playingRole);
		}
	}

}
